package com.dongnao.jack.registry.handler;

import java.util.Map;

import org.jboss.netty.util.internal.ConcurrentHashMap;

import com.dongnao.jack.spring.parse.RegistryConfig;

public class RegistryConnectionFactory {

    private static Map<RegistryConfig, RegistryConnection> map = new ConcurrentHashMap<>(5);

    public static RegistryConnection getInstance(RegistryConfig config) {

        if (config != null) {

            if (map.containsKey(config) && null != map.get(config)) {
                return map.get(config);
            } else {
                RegistryConnection connection = builderByConfig(config);
                map.put(config, connection);
                return connection;
            }

        }

        return null;

    }

    private static RegistryConnection builderByConfig(RegistryConfig config) {

        if ("redis".equals(config.getProtocol())) {
            return JedisRegistryConnection.createInstance(config);
        }

        return null;
    }

    public static Map<RegistryConfig, RegistryConnection> getMap() {
        return map;
    }

    public static void setMap(Map<RegistryConfig, RegistryConnection> map) {
        RegistryConnectionFactory.map = map;
    };

}
